//if (window.plus) {
//    shoppingcartPageReady();
//} else {
//    document.addEventListener('plusready', shoppingcartPageReady, false);
//}

$(function () {
    if (AGENT_CLIENT.WEB) {
        shoppingcartPageReady();
    }
})

var _shoppingcartPage = null;
// 初始化方法
function shoppingcartPageReady() {
    var user = null,
        html = [],
        isChecked = false,
        shopcartData = {};
        

    var shoppingcartPage = function () {
        this.init();
    };

    shoppingcartPage.prototype = {
        init: function () {
            this.initData();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (null == user) {
            	redirect("../account/login.html?backurl=shopcart|shoppingcart.html");
              return;
            }
            var requestUrl = CONFIG_MANAGER.RequstUrl + "v1/cart/" + CONFIG_MANAGER.orgId + "/" + user.id
            $.ajax({
                type: "GET",
                url: requestUrl,
                async: true,
                headers: {token: user.token},
                beforeSend: function () {
                  $.showWaiting("加载中...");
                }
            }).done(function (data) {
                _shopcartinitClass.paintCarthtml(data);
                _shoppingcartPage.bindEvent(data);
            }).fail(function (data) {
          		$.closeWaiting();
          		$.toast(data.responseJSON.Message);
          		_shopcartinitClass.paintCarthtml(null);
          		console.log( JSON.stringify(data) );            		
            }).always(function () {
              $.closeWaiting();
            });
        },

        bindEvent: function (cartData) {

            var shopcartEventClass = new shopcartEvent();

            //绑定添加减少商品数量事件
            shopcartEventClass.addOrSubNumber(cartData, user);

            //编辑购物车中的商品
            shopcartEventClass.editCart(cartData);

            //选中商品
            shopcartEventClass.checkCartProduct(user);

            //删除商品
            shopcartEventClass.deletProduct(user);

            //满减活动选择
            //shopcartEventClass.shopSubtract(cartData, user);

            //套件活动
            //shopcartEventClass.shopSuites(cartData, user);

            //去结算
            shopcartEventClass.goBalance(cartData, user);


            var shopcartAlphaClass = new shopcartEventAlpha();
						
						//跳转到商品详情
            shopcartAlphaClass.jumpToGoodsDetail();

            //优惠券选择
            shopcartAlphaClass.salesPromotion(cartData, user);
        },

    };

    var _shopcartinitClass = new shopcartInit();
    _shopcartinitClass.getFooter();

    _shoppingcartPage = new shoppingcartPage();

}

//  触发当前页
function activePage() {
  var user = getLocalStorage(STORAGE_MANAGER.user);
  if (null == user) {
  	redirect("../account/login.html?backurl=shopcart|shoppingcart.html");
    return;
  }
	var shopcartEventClass = new shopcartEvent();
	shopcartEventClass.showDeleteText(false);
}

// 重新加载页面
function reloadPage() {
	console.log( "_shoppingcartPage = " + JSON.stringify(_shoppingcartPage) );
//  if (_shoppingcartPage) {
//      _shoppingcartPage.initData();
//  }else{
    	 shoppingcartPageReady();
//  }
}
